package com.cleargroup.magic.cleara.c;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;

public class StatFsUtil {
	public static final int FREE = 0;
	public static final int TOTAL = 1;
	public static final int USED = 2;

	public static long getFreeRom() {
		return getRomSizes()[FREE];
	}

	public static long getTotalRom() {
		return getRomSizes()[TOTAL];
	}

	public static long[] getRomSizes() {
		return statBlockSize("/data");
	}


    public static long getFreeSdcard(Context context, String dir) {
        return statBlockSize(dir)[FREE];
    }

    public static long getTotalSdcard(Context context, String dir) {
        return statBlockSize(dir)[TOTAL];
    }

	public static long getTotalSdcard_sdSelect(String name) {
		return getSdcardSizes_sdSelect(name)[TOTAL];
	}

	public static long[] getSdcardSizes_sdSelect(String name) {

		return statBlockSize(name);
	}

	public static long getEnvironmentSize() {
		File localFile = Environment.getDataDirectory();
		String str = localFile.getPath();
		return statBlockSize(str)[TOTAL];
	}

	/**
	 * @param dir
	 *            the block path to be statics;
	 * 
	 * @return result[0] is free block size, result[1] is total block size;
	 */
	public static long[] statBlockSize(String dir) {
		long[] res = new long[3];
		res[FREE] = 0;
		res[USED] = 0;
		res[TOTAL] = 0;

		try {
			StatFs stat = new StatFs(dir);
			long blockSize = stat.getBlockSize();
			long availCount = stat.getAvailableBlocks();
			long totalCount = stat.getBlockCount();
			res[FREE] = availCount * blockSize;
			res[TOTAL] = totalCount * blockSize;
			res[USED] = res[TOTAL] - res[FREE];
		} catch (Throwable ignored) {
		}

		return res;
	}

	/**
	 * Use SystemClock.elapsedRealtime() instead!
	 * @see com.android.settings.deviceinfo.Status
	 * @param context
	 * @return milliseconds of boot time
	 */
	//	public static long readBootTime(Context context) {
	//		String uri = "/proc/uptime";
	//		FileReader fr = null;
	//		BufferedReader br = null;
	//		long result = 60 * 1000;
	//		try {
	//			fr = new FileReader(uri);
	//			br = new BufferedReader(fr, 8192);
	//			result = Long.valueOf(br.readLine().split("\\.")[0]) * 1000;
	//		} catch (Exception e) {
	//			LogUtil.w(TAG, e);
	//		} finally {
	//			ReleaseUtil.release(fr);
	//			ReleaseUtil.release(br);
	//		}
	//		return result;
	//	}
}
